﻿using CoreShop.App.Units.Interface;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Model.FromBody;
using CoreShop.Model.UI;
using CoreShop.Services;
using Newtonsoft.Json;
using SqlSugar.Extensions;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Yitter.IdGenerator;
using static System.Windows.Forms.AxHost;

namespace CoreShop.App
{
    public partial class frmGetUserRole: UIForm
    {
        public List<sysrolepos> checkUserRole = null;
        public sysroleinfo checkUserRoleInfo = null;
        public frmGetUserRole(GlobalEnumVars.UserRoleType userRole)
        {
            InitializeComponent();
            string roleName = EnumHelper.GetDescByValue<GlobalEnumVars.UserRoleType>((int)userRole);
            this.lblRoleName.Text = $"请给权限【{roleName}】授权" ;
        }

        private void btnClose_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void frmLogin_Load(object sender, System.EventArgs e)
        {
            txtUserCode.Text = IniFiles.ReadIniData("Local", "CheckerID").ObjToString();
        }


        private async void btnLogin_Click(object sender, System.EventArgs e)
        {
            var userSvr = new sysuserServices();
            var sysUserModel = userSvr.QueryByNameAsync(txtUserCode.Text.Trim(), Security.Encrypt(txtPassword.Text.Trim()), AppConstVars.adminPassword);
            if (sysUserModel == null) sysUserModel = await GetUserLoginRmo(txtUserCode.Text.Trim(), txtPassword.Text.Trim());
            if (sysUserModel == null) return;
            checkUserRole = ProcessUtil.GetUserRolePos(sysUserModel.id);
            checkUserRoleInfo = ProcessUtil.GetUserRoleInfo(CommFunc.ConvertToInt64(sysUserModel.posRoleId));
            IniFiles.IniWriteValue("Local", "CheckerID", txtUserCode.Text);
            DialogResult = DialogResult.OK;
        }

        /// <summary>
        /// 实时登录
        /// </summary>
        /// <param name="userCode"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        private async Task<sysuser> GetUserLoginRmo(string userCode,string password)
        {
            var rs = await SystemApiUtil.LoginByPos(userCode, password, AppConstVars.sysStore);
            if (rs.code != 0)
            {
                UIMessageBox.Show(rs.msg, "错误：", UIStyle.Green, UIMessageBoxButtons.OK);
                return null;
            }
            sysuser user = null;
            LoginBody loginBody = JsonConvert.DeserializeObject<LoginBody>(rs.data.ToString());
            if (!(new sysuserServices().SetLoginData(ref user, loginBody, AppConstVars.sysStore)))
            {
                UIMessageBox.Show("用户登录失败。", "错误：", UIStyle.Green, UIMessageBoxButtons.OK);
                return null;
            }
            if (user == null)
            {
                UIMessageBox.Show("用户登录失败。", "错误：", UIStyle.Green, UIMessageBoxButtons.OK);
                return null;
            }            
            return user;
        }
    }
}
